﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Billing;
using System.Configuration;

public class Common
{
    private static DBCommands m_db = null;
    public static DBCommands db
    {
        get
        {
            if (m_db == null)
                m_db = new DBCommands(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            return m_db;
        }
    }

    public static string MyEscape(object data)
    {
        //string test = Microsoft.Security.Application.Encoder.HtmlEncode("&<>;\"'");
        //string test = Microsoft.Security.Application.Encoder.HtmlEncode(" & ");

//<span style="font-weight: bold; ">abc<br />
//<span style="font-style: italic; ">asdfasdf<br />
//<span style="text-decoration: underline; ">sdfsadfad<br />
//</span></span></span><span style="color: #ff0000; ">dddsad<br />
//<span style="font-family: arial, helvetica, sans-serif; ">fasdf<br />
//</span></span><span style="font-size: 14pt; ">fasdfasdfasd</span>

        string[] valid_html_tags = new string[] {
            "a",
            //"abbr",
            //"acronym",
            "address",
            //"applet",
            //"area",
            "article",
            //"aside",
            "audio",
            "b",
            "base",
            "basefont",
            "bdi",
            "bdo",
            "big",
            "blockquote",
            //"body",
            "br",
            //"button",
            //"canvas",
            //"caption",
            "center",
            //"cite",
            //"code",
            //"col",
            //"colgroup",
            //"command",
            //"datalist",
            "dd",
            //"del",
            //"details",
            //"dfn",
            //"dialog",
            //"dir",
            "div",
            "dl",
            "dt",
            "em",
            //"embed",
            //"fieldset",
            //"figcaption",
            //"figure",
            "font",
            "footer",
            //"form",
            //"frame",
            //"frameset",
            "h1",
            "h2",
            "h3",
            "h4",
            "h5",
            "h6",
            //"head",
            //"header",
            "hr",
            //"html",
            "i",
            //"iframe",
            "img",
            //"input",
            //"ins",
            //"kbd",
            //"keygen",
            //"label",
            //"legend",
            "li",
            //"link",
            //"map",
            //"mark",
            //"menu",
            //"meta",
            //"meter",
            //"nav",
            //"noframes",
            //"noscript",
            //"object",
            "ol",
            //"optgroup",
            //"option",
            //"output",
            "p",
            //"param",
            "pre",
            //"progress",
            //"q",
            //"rp",
            //"rt",
            //"ruby",
            //"s",
            //"samp",
            //"script",
            //"section",
            //"select",
            "small",
            //"source",
            "span",
            "strike",
            "strong",
            "style",
            //"sub",
            //"summary",
            //"sup",
            "table",
            "tbody",
            "td",
            //"textarea",
            "tfoot",
            "th",
            "thead",
            //"time",
            //"title",
            "tr",
            //"track",
            "tt",
            "u",
            "ul",
            //"var",
            //"video",
            //"wbr",
            "o:p", //come from word
            "!--"
            };

        string[] valid_encoded_tag = new string[] {
            "&amp;",
            "&lt;",
            "&gt;",
            "&quot;",
            "&nbsp;",
            "&copy;",
            "&reg"
        };

        string[] invalid_tag = new string[] {
            ">", "&gt;",
            "<", "&lt;",
            "\"", "&quot;",
            "'", "'",
            "&", "&amp;"
        };

        string src = data == null ? "" : Convert.ToString(data);
        string dest = "";
        //string s = "";
        //for (int i = 0; i < src.Length; i++)
        //    s += "\r\n" + i.ToString() + ":'" + src.Substring(i, 1) + "'";
        int pos = 0;
        bool in_html_tag = false;
        while (pos < src.Length)
        {
            bool done = false;

            //ignore tag <o:p>
            if (LeftCompare(src, pos, "<o:p>"))
            {
                dest += "";
                pos += 5;
                done = true;
            }
            //ignore tag </o:p>
            else if (LeftCompare(src, pos, "</o:p>"))
            {
                dest += "";
                pos += 6;
                done = true;
            }
            //allow comment <!--            
            else if (LeftCompare(src, pos, "<!--"))
            {
                dest += "<!--";
                pos += 4;
                done = true;
                in_html_tag = true;
            }

            //allow html tag
            if (!done)
            {
                for (int i = 0; i < valid_html_tags.Length; i++)
                {
                    if (LeftCompare(src, pos, "<" + valid_html_tags[i] + " />"))
                    {
                        dest += "<" + valid_html_tags[i] + " />";
                        pos += valid_html_tags[i].Length + 4;
                        done = true;
                        break;
                    }
                    else if (LeftCompare(src, pos, "<" + valid_html_tags[i] + "/>"))
                    {
                        dest += "<" + valid_html_tags[i] + "/>";
                        pos += valid_html_tags[i].Length + 3;
                        done = true;
                        break;
                    }
                    else if (LeftCompare(src, pos, "<" + valid_html_tags[i] + ">"))
                    {
                        dest += "<" + valid_html_tags[i] + ">";
                        pos += valid_html_tags[i].Length + 2;
                        done = true;
                        break;
                    }
                    else if (LeftCompare(src, pos, "<" + valid_html_tags[i] + " "))
                    {
                        dest += "<" + valid_html_tags[i] + " ";
                        pos += valid_html_tags[i].Length + 2;
                        done = true;
                        in_html_tag = true;
                        break;
                    }
                    else if (LeftCompare(src, pos, "</" + valid_html_tags[i] + ">"))
                    {
                        dest += "</" + valid_html_tags[i] + ">";
                        pos += valid_html_tags[i].Length + 3;
                        done = true;
                        break;
                    }
                }
            }

            //allow encoded tag
            if (!done)
            {
                for (int i = 0; i < valid_encoded_tag.Length; i++)
                {
                    if (LeftCompare(src, pos, valid_encoded_tag[i]))
                    {
                        dest += valid_encoded_tag[i];
                        pos += valid_encoded_tag[i].Length;
                        done = true;
                        break;
                    }
                }                
            }

            //allow '>' and '"' in html tag
            if (!done)
            {
                if (src.Substring(pos, 1) == ">" && in_html_tag)
                {
                    dest += ">";
                    pos += 1;
                    done = true;
                    in_html_tag = false;
                }
                else if (src.Substring(pos, 1) == "\"" && in_html_tag)
                {
                    dest += "\"";
                    pos += 1;
                    done = true;
                }
            }

            //escape invalid tag
            if (!done)
            {
                for (int i = 0; i < invalid_tag.Length - 1; i += 2)
                {
                    if (LeftCompare(src, pos, invalid_tag[i]))
                    {
                        dest += invalid_tag[i + 1];
                        pos += invalid_tag[i].Length;
                        done = true;
                        break;
                    }
                }
            }

            //allow other char
            if (!done)
            {
                dest += src.Substring(pos, 1);
                pos += 1;
                done = true;
            }
        }
        return dest;
    }

    public static bool LeftCompare(string s, int start, string left)
    {
        return LeftStr(s, start, left.Length).ToUpper() == left.ToUpper();
    }

    public static string LeftStr(string s, int start, int length)
    {
        if (s.Length - start >= length)
            return s.Substring(start, length);
        else
            return s.Substring(start, s.Length - start);
    }



    public static string htmlencode(string str)
    {
        if (str == null || str == "")
            return "";
        str = str.Replace(">", "&gt;");
        str = str.Replace(" <", "&lt;");
        str = str.Replace(" ", "&nbsp;");
        str = str.Replace("  ", " &nbsp;");
        str = str.Replace("\"", "&quot;");
        str = str.Replace("\'", "&#39;");
        str = str.Replace("\n", " <br/> ");
        return str;
    }
}
